package com.example.fashion.android.FourCompoents.activity;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.fashion.android.R;
import com.example.fashion.android.base.BaseActivity.BaseActivity;
import com.example.fashion.android.FourCompoents.adapter.ReadContactAdapter;

import java.util.ArrayList;
import java.util.List;

public class ContactActivity extends BaseActivity implements View.OnClickListener{


    //游标对象
    Cursor mCursor;
    RecyclerView mRecyclerView;
    //联系人列表适配器
    ReadContactAdapter mReadContactAdapter;
    //联系人数据集合
    List mConTactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_contact);
    }


    @Override
    public void initView() {
        //显示标题栏
        setSystemTitleName("联系人列表");

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);



    }

    @Override
    public void initData() {
        //初始化数据
        mConTactList = new ArrayList<>();

        //读取联系人
        readContact();

        //绑定适配器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mReadContactAdapter = new ReadContactAdapter(mRecyclerView, mConTactList, R.layout.item_read_contact);
        mRecyclerView.setAdapter(mReadContactAdapter);


    }


    /**
     * 读取联系人
     */
    public void readContact() {
        try {
            mCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (mCursor != null) {
                mConTactList.clear();
                while (mCursor.moveToNext()) {
                    String name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    mConTactList.add(name + " " + number);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCursor != null) {
                mCursor.close();
            }
        }

    }


    @Override
    public void onClick(View view) {

    }


    @Override
    protected void clear() {

    }

}
